package fisio.control;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import fisio.global.Const;
import fisio.model.Atendimento;
import fisio.model.Prontuario;
import fisio.service.ServicoAtendimento;

public class AtendimentoServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {	

		String op = request.getParameter("op");
		
		if(op != null) {
			
			if(op.equals("listar_f")) {
				
				Vector<Atendimento> temp = ServicoAtendimento.getAtendimentosFisioterapeuta(null);
				JSONArray array = new JSONArray();
				
				for(int i = 0; i < temp.size(); i++)
					array.add(temp.get(i).toJSON());
				
				response.setContentType("application/json");
				response.getWriter().print(array);
			} else if(op.equals("listar_p")){
				
				Vector<Atendimento> temp = ServicoAtendimento.getAtendimentosPaciente(null);
				JSONArray array = new JSONArray();
				
				for(int i = 0; i < temp.size(); i++)
					array.add(temp.get(i).toJSON());
				
				response.setContentType("application/json");
				response.getWriter().print(array);
			}
			
		} else
			response.sendRedirect(Const.CONTEXT);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		
		int id_f = Integer.parseInt(request.getParameter("id_f"));
		int id_p = Integer.parseInt(request.getParameter("id_p"));
		
		ServicoAtendimento.insertAtendimento(new Atendimento(id_f, id_p, new Vector<Prontuario>()));
		response.sendRedirect(Const.PAGE_HOME_LOGADO);
	}
}